package com.meihua.shellapp.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.utils.Utils;
import com.orhanobut.logger.Logger;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 添加银行卡信息
 */

public class AddBankInfoActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.add_bank_info_next)
    TextView add_bank_info_next;
    private EditText add_bank_info_number;//卡号
    private EditText add_bank_info_name;//持卡人姓名

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_bank_info;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        add_bank_info_name = (EditText) findViewById(R.id.add_bank_info_name);
        add_bank_info_number = (EditText) findViewById(R.id.add_bank_info_number);
        tvTitle.setVisibility(View.VISIBLE);
        mIVTopLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("添加银行卡");

        add_bank_info_name.addTextChangedListener(textWatcher);
        add_bank_info_number.addTextChangedListener(textWatcher);
    }

    /**
     * 输入框监听
     */
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!add_bank_info_name.getText().toString().equals("") &&
                    (add_bank_info_number.getText().toString().length() >= 12)) {
                add_bank_info_next.setEnabled(true);
            } else {
                add_bank_info_next.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };


    @OnClick({R.id.iv_top_left, R.id.add_bank_info_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_top_left:
                finish();
                break;
            case R.id.add_bank_info_next:
                String name = add_bank_info_name.getText().toString().trim();
                String number = add_bank_info_number.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(number)) {
                    Toast.makeText(this, "请输入正确的银行卡", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent in = new Intent(AddBankInfoActivity.this, AddBankInfoPhoneActivity.class);
                in.putExtra("real_name", name);
                in.putExtra("banks_card", number);
                startActivityForResult(in, 11);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (resultCode == Constant.ADDBANK_ID) {
                finish();
            }
        }
    }
}
